package tw.asap.plus.network.config;


import com.squareup.okhttp.OkHttpClient;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import retrofit.ErrorHandler;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import tw.asap.plus.BuildConfig;
import tw.asap.plus.common.ConstValue;

/**
 * 简单的 Retrofit Service 创建者
 * <p>
 * Created by LiFZhe on 3/11/15.
 */
public class HRetrofitCreator {
    private RestAdapter mRestAdapter;
    private QfEndpoint myEndpoint;
    private static HRetrofitCreator mHRetrofitCreator;

    public static HRetrofitCreator getInstance() {
        if (mHRetrofitCreator == null) {
            synchronized (HRetrofitCreator.class) {
                if (mHRetrofitCreator == null)
                    if (BuildConfig.DEBUG) {
                        mHRetrofitCreator = new HRetrofitCreator(ConstValue.TEST_URL, new HRetrofitInterceptor(), new HErrorHandler(), BuildConfig.DEBUG);

                    } else {
                        mHRetrofitCreator = new HRetrofitCreator(ConstValue.SERVER_URL, new HRetrofitInterceptor(), new HErrorHandler(), BuildConfig.DEBUG);
                    }
            }
        }
        return mHRetrofitCreator;
    }


    private HRetrofitCreator(String endPoint, RequestInterceptor interceptor, ErrorHandler errorHandler, boolean isTesting) {
        RestAdapter.LogLevel logLevel = RestAdapter.LogLevel.FULL;
        if (!isTesting) {
            logLevel = RestAdapter.LogLevel.NONE;
        }

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        okHttpClient.setConnectTimeout(20 * 1000, TimeUnit.MILLISECONDS);
        okHttpClient.setReadTimeout(20 * 1000, TimeUnit.MILLISECONDS);
//        okHttpClient.networkInterceptors().add(new AsapIntercepter());
        RestAdapter.Log log = System.out::println;

        myEndpoint = new QfEndpoint();
        myEndpoint.setUrl(endPoint);

        if (ConstValue.IS_UNIT_TESTING) {
            mRestAdapter = new RestAdapter.Builder()
                    .setEndpoint(myEndpoint)
                    .setRequestInterceptor(interceptor)
                    .setErrorHandler(errorHandler)
                    .setClient(new OkClient(okHttpClient))
                    .setLogLevel(logLevel)
                    .setLog(log)
                    .build();
        } else {
            mRestAdapter = new RestAdapter.Builder()
                    .setEndpoint(myEndpoint)
                    .setRequestInterceptor(interceptor)
                    .setErrorHandler(errorHandler)
                    .setClient(new OkClient(okHttpClient))
                    .setLogLevel(RestAdapter.LogLevel.NONE)
                    .build();
        }
    }

    public void setEndpoint(String baseUrl) {
        myEndpoint.setUrl(baseUrl);
    }


    public <T> T getService(Class<T> serviceClazz) {
        return mRestAdapter.create(serviceClazz);
    }


}
